package com.vogue.supplymall.order.controller;

import com.vogue.supplymall.common.domain.dto.AlterRefundInfo;
import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.common.domain.tool.MessageSourceUtils;
import com.vogue.supplymall.order.common.CheckRefundUtils;
import com.vogue.supplymall.order.domain.biz.RefundBiz;
import com.vogue.supplymall.order.exception.AddOrderException;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * The type Refund add controller.
 * 添加退货订单处理controller
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-03-16
 */
@RestController
public class RefundAddController extends BaseController {
    /**
     * The constant log.
     */
    public static final Logger LOG = LoggerFactory.getLogger(RefundAddController.class);

    @Resource
    private RefundBiz refundBiz;
    @Resource
    private MessageSourceUtils messageSourceUtils;


    /**
     * Add refund response entity.
     *
     * @param appver     the appver
     * @param refundInfo the refund info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/refund/add", method = RequestMethod.POST)
    public ResponseEntity<Object> addRefund(@PathVariable("appver") String appver, @RequestBody AlterRefundInfo refundInfo) {
        LOG.info("add refund start >>>>>> ");
        UserInfo info = new UserInfo();
        if (SecurityUtils.getSubject().isAuthenticated()) {
            info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            LOG.info("获取登信息：" + info.getLoginId());
            if (CheckRefundUtils.checkAddRefundParameter(refundInfo, info.getLoginUserType())) {
                LOG.info(" 参数错误！ ");
                throw new AddOrderException("parameter error");
            }
        } else {
            LOG.info("获取登信息失败！");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        refundBiz.addRefund(refundInfo, info);
        resultInfo.setResult(true);
        resultInfo.setData(null);
        /* 返回订单提交成功 */
        resultInfo.setMsg(messageSourceUtils.getMessage("2608", new String[]{}));
        LOG.info(" <<<<<< add refund end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

}
